<!-- 无害化处理 -->
<template>
  <div class='com-container'>
    <div class='com-chart' ref='ranksss_ref'></div>
  </div>
</template>

<script>
import axios from 'axios'
import { mapState } from 'vuex'
import Rank from '@/assets/static/data/ranksss.json'

export default {
  data () {
    return {
      chartInstance: null,
      allData: null,
      startValue: 0, // 区域缩放的起点值
      endValue: 12, // 区域缩放的终点值
      timerId: null // 定时器的标识
    }
  },
  mounted () {
    this.initChart()
    this.getData()
    window.addEventListener('resize', this.screenAdapter)
    this.screenAdapter()
  },
  destroyed () {
    window.removeEventListener('resize', this.screenAdapter)
    clearInterval(this.timerId)
  },
  methods: {
    initChart () {
      this.chartInstance = this.$echarts.init(this.$refs.ranksss_ref, this.theme)
      const initOption = {
      /*  title: {
          text: '▎ 无害化处理',
          textStyle:{
            fontFamily:'华文楷体, serif',
            fontSize: 20
          },
          left: 20,
          top: 20
        },*/
        grid: {
          top: '20%',
          left: '5%',
          right: '5%',
          bottom: '5%',
          containLabel: true
        },
        tooltip: {
          show: true
        },
        xAxis: {
          type: 'category',
          axisLabel:{
            interval:0,
            rotate:40
          }
        },
        yAxis: {
          type: 'value',
          interval: 50,
        },
        series: [
          {
            type: 'bar'
          }
        ]
      }
      this.chartInstance.setOption(initOption)
      this.chartInstance.on('mouseover', () => {
        clearInterval(this.timerId)
      })
      this.chartInstance.on('mouseout', () => {
        this.startInterval()
      })
    },
    async getData () {
      // 获取服务器的数据, 对this.allData进行赋值之后, 调用updateChart方法更新图表
      const  ret  = Rank
      this.allData = ret
      // 对allData里面的每一个元素进行排序, 从大到小进行
     /* this.allData.sort((a, b) => {
        return b.value - a.value
      })*/
      console.log(this.allData)
      this.updateChart()
      this.startInterval()
    },
    updateChart () {
      const colorArr = [
        ['red', 'green'],
        ['red', 'green'],
        ['red', 'green']
      ]
      // 处理图表需要的数据
      // 所有省份所形成的数组
      const provinceArr = this.allData.map(item => {
        return item.name
      })
      // 所有省份对应的销售金额
      const valueArr = this.allData.map(item => {
        return item.value
      })
      // const dataOption = {
      //   xAxis: {
      //     data: provinceArr
      //   },
      //   dataZoom: {
      //     show: false,
      //     startValue: this.startValue,
      //     endValue: this.endValue
      //   },
      //   series: [
      //     {
      //       data: valueArr,
      //       itemStyle: {
      //         color: arg => {
      //           let targetColorArr = null
      //           if (arg.value > 100) {
      //             targetColorArr = colorArr[0]
      //           } else if (arg.value > 50) {
      //             targetColorArr = colorArr[1]
      //           } else {
      //             targetColorArr = colorArr[2]
      //           }
      //           return new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
      //             {
      //               offset: 0,
      //               color: targetColorArr[0]
      //             },
      //             {
      //               offset: 1,
      //               color: targetColorArr[1]
      //             }
      //           ])
      //         }
      //       }
      //     }
      //   ]
      // }
      const option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            label: {
              backgroundColor: 'red'
            }
          }
        },
        color: ['#00DDFF', '#37A2FF', '#FF0087', '#FFBF00'],
        legend: {
          data: ['无害化处理']
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [
          {
            type: 'category',
            boundaryGap: false,
            data: provinceArr
          }
        ],
        yAxis: [
          {
            type: 'value'
          }
        ],
        series: [
          {
            name: '无害化处理',
            type: 'line',
            stack: 'Total',
            areaStyle: {
              color: {
                type: 'linear',
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [{
                  offset: 0,  color: 'rgba(250,166,47,1)'  // 100% 处的颜色
                }, {
                  offset: 1, color: 'rgba(102,183,187,.1' //   0% 处的颜色
                }],
                global: false // 缺省为 false
              },
            },
            color: 'rgba(250,166,47,1)', //线条颜色
            smooth: true, //线条平滑
            emphasis: {
              focus: 'series'
            },
            data: valueArr
          }
        ]
      }
      this.chartInstance.setOption(option)
    },
    screenAdapter () {
      const titleFontSize = this.$refs.ranksss_ref.offsetWidth / 100 * 3.6
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize
          }
        },
        series: [
          {
            barWidth: titleFontSize,
            itemStyle: {
              barBorderRadius: [titleFontSize / 2, titleFontSize / 2, 0, 0]
            }
          }
        ]
      }
      this.chartInstance.setOption(adapterOption)
      this.chartInstance.resize()
    },
    startInterval () {
      if (this.timerId) {
        clearInterval(this.timerId)
      }
      this.timerId = setInterval(() => {
        this.startValue++
        this.endValue++
        if (this.endValue > this.allData.length - 1) {
          this.startValue = 0
          this.endValue = 12
        }
        this.updateChart()
      }, 2000)
    }
  },
  computed: {
    ...mapState(['theme'])
  },
  watch: {
    theme () {
      console.log('主题切换了')
      this.chartInstance.dispose() // 销毁当前的图表
      this.initChart() // 重新以最新的主题名称初始化图表对象
      this.screenAdapter() // 完成屏幕的适配
      this.updateChart() // 更新图表的展示
    }
  }
}
</script>

<style lang='less' scoped>
</style>
